package pstube.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import pstube.download.Download;
import pstube.languages.Lang;
import pstube.languages.LangEnglish;
import pstube.languages.LangGerman;

import enums.AppletNameValues;
import enums.Language;
import enums.txtValues;

public class AppletGui {
	
	private static JButton downloadbutton;
	private static JTextField sourcefield;
	private static JTextField destinationfield;
	private static JTextField filenamefield;
	private static JLabel infofield;
	
	/* Config */
	private static GuiConfig guiConfig = new GuiConfig();
	private static Dimension appletDimension = guiConfig.getAppletDimension();
	private static Dimension inputDimension  = guiConfig.getInputDimension();
	private static Dimension buttonDimension = guiConfig.getButtonDimension();
	private static String appletName         = guiConfig.getAppleName(AppletNameValues.FullName);
	private static Language language         = guiConfig.getLanguage();
	private static Lang lng                   = new LangGerman();
	
	private static GUI gui = null;
	
	
	@SuppressWarnings("static-access")
	public AppletGui(GUI gui) {
		this.gui = gui;
		
		/* create GUI */
		JPanel inputPanel = createPanel(appletDimension);
		JFrame frame = createFrame(appletName, appletDimension);
		@SuppressWarnings("unused")
		Container container = createContainer(frame, appletDimension, inputPanel);
		
		/* select language */
		switch(language){
			case german  : lng = new LangGerman(); break;
			case english : lng = new LangEnglish(); break;
			default: lng = new LangGerman(); break;
		}
	}
	
	private Container createContainer(JFrame frame, Dimension dim, JPanel panel){
		Container appletContainer = frame.getContentPane();				// Container from Frame
		setContainerDimension(appletContainer, dim);
		appletContainer.setLayout(new BorderLayout());
		appletContainer.add(panel, BorderLayout.CENTER);
		return appletContainer;
	}
	
	private JFrame createFrame(String name, Dimension dim){
		JFrame frame = createMainFrame(name);
		setFrameDimension(frame, dim);
		frame.setDefaultCloseOperation(this.onCloseOperation());
		frame.pack();
		frame.setVisible(true);
		return frame;
	}

	private JPanel createPanel(Dimension dim){
		JPanel inputPanel = createInputPanel();
		setPanelDimension(inputPanel, dim);
		return inputPanel;
	}
	
	/*
	 * Will tell us what to do on close applet
	 * Would be nice to check for running downloads and 
	 **/
	private int onCloseOperation(){
		return WindowConstants.EXIT_ON_CLOSE;
	}
	
	private static JPanel createInputPanel(){
		JPanel inputPanel = new JPanel();
		BoxLayout layout = new BoxLayout(inputPanel, BoxLayout.Y_AXIS);
		inputPanel.setLayout(layout);

		/* source field */
		downloadbutton = getDlButton();			// button download
		sourcefield = new JTextField();			// input source
		destinationfield = new JTextField();	// input destination
		filenamefield = new JTextField();		// input filename
		setTextFieldDimension(sourcefield, inputDimension);
		setTextFieldDimension(destinationfield, inputDimension);
		setTextFieldDimension(filenamefield, inputDimension);
		setButtonDimension(downloadbutton, buttonDimension);
		
		/* set captions */
		sourcefield.setText(lng.getText(txtValues.DownloadFieldText));
		destinationfield.setText(lng.getText(txtValues.DestinationFieldText));
		filenamefield.setText(""); // could left blank
		downloadbutton.setText(lng.getText(txtValues.DownloadButtonText));
		
		//lng.getText(txtValues.)
		
		/* info field */
		infofield = new JLabel();
		infofield.setText(lng.getText(txtValues.MsgStartup));
		
		/* place all fields */
		JPanel fieldPanel = new JPanel();
		fieldPanel.setLayout(new GridLayout(8,2));
		
		/* info field */
		fieldPanel.add(new JLabel("Informationen"));
		fieldPanel.add(infofield);
		/* source field */
		fieldPanel.add(new JLabel(lng.getText(txtValues.DownloadFieldCaption)));
		fieldPanel.add(sourcefield);
		/* destination field */
		fieldPanel.add(new JLabel(lng.getText(txtValues.DestinationFieldCaption)));
		fieldPanel.add(destinationfield);
		/* file name field */
		fieldPanel.add(new JLabel(lng.getText(txtValues.FilenameFieldCaption)));
		fieldPanel.add(filenamefield);
		/* free space */
		fieldPanel.add(new JLabel(""));
		fieldPanel.add(new JLabel(""));
		/* download button */
		fieldPanel.add(new JLabel(""));
		fieldPanel.add(downloadbutton);
		
		inputPanel.add(fieldPanel);
		return inputPanel;
		
	}
	
	/*
	 * handles given destinations
	 * removes slashes at the end of the destination
	 * @input String unformatedDestination
	 * @output String formatedDestination
	 */
	@SuppressWarnings("unused")
	private static String formatDestination(String dest){
		int length = dest.length();
		char[] chars = dest.toCharArray();
		String result = "";
		for(int i = 0; i < length; i++){
			if(i!=length-1 || (chars[i]!='/' && chars[i]!='\\')){result +=  chars[i];}
		}
		return result;
	}

	private JFrame createMainFrame(String name){
		return new JFrame(name);
	}
	
	private static void setContainerDimension(Container con, Dimension dim){
		con.setSize(dim);
		con.setMinimumSize(dim);
		con.setMaximumSize(dim);
	}
	
	private static void setButtonDimension(JButton btn, Dimension dim){
		btn.setSize(dim);
		btn.setMinimumSize(dim);
		btn.setMaximumSize(dim);
	}

	private static void setTextFieldDimension(JTextField tf, Dimension dim){
		tf.setSize(dim);
		tf.setMinimumSize(dim);
		tf.setMaximumSize(dim);
	}
	
	private static void setFrameDimension(JFrame jf, Dimension dim){
		jf.setSize(dim);
		jf.setMinimumSize(dim);
		jf.setMaximumSize(dim);
	}
	
	private static void setPanelDimension(JPanel jp, Dimension dim){
		jp.setSize(dim);
		jp.setMinimumSize(dim);
		jp.setMaximumSize(dim);	
	}
	
	/*
	 * creation of the download button
	 */
	
	private static JButton getDlButton(){
		
		downloadbutton = new JButton();
		
		ActionListener al = new ActionListener(){
			
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				String source = sourcefield.getText();
				String destination = destinationfield.getText();
				String filename = filenamefield.getText();
				destination += File.separator+filename+".flv";
				if(!source.isEmpty() && !destination.isEmpty() && !filename.isEmpty()){
					Download dl = new Download(source, destination, false);
					gui.addDownload(dl);
					sourcefield.setText("");
					destinationfield.setText("");
					filenamefield.setText("");
					infofield.setText(lng.getText(txtValues.InfoDownloadStarted) + " " + filename);
				}else{
					infofield.setText(lng.getText(txtValues.InfoFieldError));
					System.out.println("Empty Action");
				}
				// should perform GUI Alert
				// TODO Auto-generated method stub
				
			}

		};
		
		downloadbutton.addActionListener(al);
		return downloadbutton;
	}

	public GUI getGui() {
		return gui;
	}

	public void writeMsg(String string) {
		infofield.setText(string);		
	}
	
}
